package com.xindaoapp.happypet.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;

import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.adapter.CommonViewPagerAdapter;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.fragments.MyPostFragment;
import com.xindaoapp.happypet.fragments.MyReplyFragment;
import com.xindaoapp.happypet.ui.GlaftActivity;
import com.xindaoapp.happypet.utils.Constants;

/**
 * 设置-我的帖子
 * 
 * @author chenghao
 * 
 */
public class MyPostActivity extends com.xindaoapp.happypet.ui.BaseActivity implements
        OnClickListener {
    private ViewPager vViewPager;
    private View rl_point;
    private RefreshBroadCast refreshBroadCast;

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_mypost;
    }

    @Override
    protected void initViews() {
        super.initViews();
        refreshBroadCast = new RefreshBroadCast();
        registerReceiver(refreshBroadCast, new IntentFilter(Constants.REFRESHPOSTINFO));

        vViewPager = (ViewPager) findViewById(R.id.viewpager);
        rl_point = findViewById(R.id.rl_point);
        View iv_mypoint = findViewById(R.id.iv_mypoint);
        if (HappyPetApplication.get().getUploaQueueImpl().getPostQueue().size() > 0) {
            iv_mypoint.setVisibility(View.VISIBLE);
        } else {
            iv_mypoint.setVisibility(View.GONE);
        }


        Fragment[] fragments =
                new Fragment[] {MyPostFragment.newInstance(false),
                        MyReplyFragment.newInstance(false)};
        View[] tabViews =
                new View[] {findViewById(R.id.tab_left_view), findViewById(R.id.tab_right_view)};
        CommonViewPagerAdapter adapter =
                new CommonViewPagerAdapter(getSupportFragmentManager(), fragments, tabViews);
        for (int i = 0; i < tabViews.length; i++) {
            final int temp = i;
            tabViews[i].setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    vViewPager.setCurrentItem(temp);
                }
            });
        }
        vViewPager.setAdapter(adapter);
        vViewPager.setOffscreenPageLimit(2);
        vViewPager.setOnPageChangeListener(adapter);
    }

    @Override
    protected void initEvents() {
        super.initEvents();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
        }
    }

    @Override
    protected OnClickListener getLeftViewClickListener() {
        return new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        };
    }

    @Override
    protected int getTopBarLeftImageViewRes() {
        return R.drawable.top_bar_back;
    }

    @Override
    protected String getTopBarTitleStrRes() {
        return "我的帖子";
    }

    // @Override
    // protected int getTopBarRightImageViewRes() {
    // if (HappyPetApplication.get().getUploaQueueImpl().getPostQueue().size() == 0)
    // return 0;
    // else
    // return R.drawable.glaft;
    // }

    @Override
    protected int getTopBarRightTextViewRes() {
        if (HappyPetApplication.get().getUploaQueueImpl().getPostQueue().size() == 0)
            return 0;
        else
            return R.string.draft;
    }

    @Override
    protected OnClickListener getRightViewClickListener() {
        return new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyPostActivity.this, GlaftActivity.class));
            }
        };
    }

    class RefreshBroadCast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (rl_point != null
                    && HappyPetApplication.get().getUploaQueueImpl().getPostQueue().size() == 0) {
                rl_point.setVisibility(View.GONE);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (refreshBroadCast != null)
            unregisterReceiver(refreshBroadCast);
    }


}
